namespace QMVnSoft.Invoices
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.Controls.ChildGridControls;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormCTDHF0Editor : FormControl
    {
        private BtnControl btnClose;
        private BtnControl btnMakeCommand;
        private BtnControl btnMakeRequire;
        private DataGridViewTextBoxColumn Column1;
        private NumericColumn Column10;
        private NumericColumn Column11;
        private NumericColumn Column12;
        private NumericColumn Column13;
        private DataGridViewCheckBoxColumn Column14;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column3;
        private NumericColumn Column4;
        private NumericColumn Column5;
        private NumericColumn Column6;
        private NumericColumn Column7;
        private NumericColumn Column8;
        private NumericColumn Column9;
        private IContainer components;
        private GridControl grdDetail;
        private bool ib_CheckAccepted;
        private object is_MasterKey;
        private HeaderCheck oDetailHeader;
        private DataTable oLocalDetailTable;
        private DataRow oLocalMasterRow;
        private CTDHDataAccess oMethod = new CTDHDataAccess();

        public FormCTDHF0Editor(object ps_MasterKey, DataRow po_MasterRow)
        {
            this.InitializeComponent();
            this.MasterKey = ps_MasterKey;
            this.LocalMasterRow = po_MasterRow;
            this.grdDetail.AutoGenerateColumns = false;
            this.oDetailHeader = new HeaderCheck();
            this.Column14.HeaderCell = this.oDetailHeader;
            this.Column14.HeaderText = "";
            this.oDetailHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(this.oDetailHeader_OnCheckBoxClicked);
            this.OnFillData();
            base.ResizeForm();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnMakeCommand_Click(object sender, EventArgs e)
        {
            new FormCTSXEditor(2, this.LocalMasterRow, this.LocalDetailTable).Show();
        }

        private void btnMakeRequire_Click(object sender, EventArgs e)
        {
            new FormCTYCMEditor(2, this.LocalMasterRow, this.LocalDetailTable).Show();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void grdDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataRow row = ((DataRowView) this.DetailGrid.CurrentRow.DataBoundItem).Row;
        }

        private void InitializeComponent()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            DataGridViewCellStyle style2 = new DataGridViewCellStyle();
            DataGridViewCellStyle style3 = new DataGridViewCellStyle();
            DataGridViewCellStyle style4 = new DataGridViewCellStyle();
            DataGridViewCellStyle style5 = new DataGridViewCellStyle();
            DataGridViewCellStyle style6 = new DataGridViewCellStyle();
            DataGridViewCellStyle style7 = new DataGridViewCellStyle();
            DataGridViewCellStyle style8 = new DataGridViewCellStyle();
            DataGridViewCellStyle style9 = new DataGridViewCellStyle();
            DataGridViewCellStyle style10 = new DataGridViewCellStyle();
            this.btnMakeRequire = new BtnControl();
            this.btnMakeCommand = new BtnControl();
            this.btnClose = new BtnControl();
            this.grdDetail = new GridControl();
            this.Column1 = new DataGridViewTextBoxColumn();
            this.Column2 = new DataGridViewTextBoxColumn();
            this.Column3 = new DataGridViewTextBoxColumn();
            this.Column4 = new NumericColumn();
            this.Column5 = new NumericColumn();
            this.Column6 = new NumericColumn();
            this.Column7 = new NumericColumn();
            this.Column8 = new NumericColumn();
            this.Column9 = new NumericColumn();
            this.Column10 = new NumericColumn();
            this.Column11 = new NumericColumn();
            this.Column12 = new NumericColumn();
            this.Column13 = new NumericColumn();
            this.Column14 = new DataGridViewCheckBoxColumn();
            ((ISupportInitialize) this.grdDetail).BeginInit();
            base.SuspendLayout();
            this.btnMakeRequire.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnMakeRequire.Font = new Font("Arial", 8.25f);
            this.btnMakeRequire.Location = new Point(0x170, 12);
            this.btnMakeRequire.Name = "btnMakeRequire";
            this.btnMakeRequire.Size = new Size(0x86, 0x17);
            this.btnMakeRequire.TabIndex = 0;
            this.btnMakeRequire.Text = "Lập yêu cầu mua hàng";
            this.btnMakeRequire.UseVisualStyleBackColor = true;
            this.btnMakeRequire.Click += new EventHandler(this.btnMakeRequire_Click);
            this.btnMakeCommand.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnMakeCommand.Font = new Font("Arial", 8.25f);
            this.btnMakeCommand.Location = new Point(0x1fc, 12);
            this.btnMakeCommand.Name = "btnMakeCommand";
            this.btnMakeCommand.Size = new Size(0x86, 0x17);
            this.btnMakeCommand.TabIndex = 1;
            this.btnMakeCommand.Text = "Lập lệnh sản xuất";
            this.btnMakeCommand.UseVisualStyleBackColor = true;
            this.btnMakeCommand.Click += new EventHandler(this.btnMakeCommand_Click);
            this.btnClose.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnClose.Font = new Font("Arial", 8.25f);
            this.btnClose.Location = new Point(0x288, 12);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new Size(0x86, 0x17);
            this.btnClose.TabIndex = 2;
            this.btnClose.Text = "Quay ra";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new EventHandler(this.btnClose_Click);
            this.grdDetail.AllowUserToAddRows = false;
            this.grdDetail.AllowUserToDeleteRows = false;
            this.grdDetail.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.grdDetail.CheckAutoSize = true;
            this.grdDetail.CheckColor = false;
            this.grdDetail.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdDetail.Columns.AddRange(new DataGridViewColumn[] { this.Column1, this.Column2, this.Column3, this.Column4, this.Column5, this.Column6, this.Column7, this.Column8, this.Column9, this.Column10, this.Column11, this.Column12, this.Column13, this.Column14 });
            this.grdDetail.Font = new Font("Arial", 8.25f);
            this.grdDetail.Location = new Point(0, 0x2d);
            this.grdDetail.Name = "grdDetail";
            this.grdDetail.Size = new Size(0x31a, 0x1eb);
            this.grdDetail.TabIndex = 3;
            this.Column1.DataPropertyName = "Ma_Vt";
            this.Column1.HeaderText = "Vật tư";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Width = 0x35;
            this.Column2.DataPropertyName = "Ten_Vt";
            this.Column2.HeaderText = "Tên vật tư";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Width = 120;
            this.Column3.DataPropertyName = "Dvt";
            this.Column3.HeaderText = "Đvt";
            this.Column3.Name = "Column3";
            this.Column3.ReadOnly = true;
            this.Column3.Width = 0x1c;
            this.Column4.DataPropertyName = "So_Luong0";
            style.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column4.DefaultCellStyle = style;
            this.Column4.HeaderText = "Đặt hàng";
            this.Column4.Name = "Column4";
            this.Column4.ReadOnly = true;
            this.Column4.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column4.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column4.Width = 0x35;
            this.Column5.DataPropertyName = "So_Luong1";
            style2.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column5.DefaultCellStyle = style2;
            this.Column5.HeaderText = "Thực hiện";
            this.Column5.Name = "Column5";
            this.Column5.ReadOnly = true;
            this.Column5.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column5.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column5.Width = 0x35;
            this.Column6.DataPropertyName = "Ton_Kho1";
            style3.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column6.DefaultCellStyle = style3;
            this.Column6.HeaderText = "Tồn kho";
            this.Column6.Name = "Column6";
            this.Column6.ReadOnly = true;
            this.Column6.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column6.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column6.Width = 0x35;
            this.Column7.DataPropertyName = "Ton_Kho2";
            style4.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column7.DefaultCellStyle = style4;
            this.Column7.HeaderText = "Đơn hàng";
            this.Column7.Name = "Column7";
            this.Column7.ReadOnly = true;
            this.Column7.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column7.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column7.Width = 0x35;
            this.Column8.DataPropertyName = "Ton_Kho3";
            style5.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column8.DefaultCellStyle = style5;
            this.Column8.HeaderText = "Thành phẩm";
            this.Column8.Name = "Column8";
            this.Column8.ReadOnly = true;
            this.Column8.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column8.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column8.Width = 0x35;
            this.Column9.DataPropertyName = "Ton_Kho4";
            style6.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column9.DefaultCellStyle = style6;
            this.Column9.HeaderText = "Yêu cầu nhập";
            this.Column9.Name = "Column9";
            this.Column9.ReadOnly = true;
            this.Column9.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column9.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column9.Width = 0x35;
            this.Column10.DataPropertyName = "Ton_Kho5";
            style7.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column10.DefaultCellStyle = style7;
            this.Column10.HeaderText = "Yêu cầu xuất";
            this.Column10.Name = "Column10";
            this.Column10.ReadOnly = true;
            this.Column10.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column10.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column10.Width = 0x35;
            this.Column11.DataPropertyName = "Ton_Kho0";
            style8.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column11.DefaultCellStyle = style8;
            this.Column11.HeaderText = "Tổng";
            this.Column11.Name = "Column11";
            this.Column11.ReadOnly = true;
            this.Column11.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column11.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column11.Width = 0x35;
            this.Column12.DataPropertyName = "So_Luong2";
            style9.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column12.DefaultCellStyle = style9;
            this.Column12.HeaderText = "Còn lại";
            this.Column12.Name = "Column12";
            this.Column12.ReadOnly = true;
            this.Column12.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column12.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column12.Width = 0x35;
            this.Column13.DataPropertyName = "So_Luong";
            style10.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column13.DefaultCellStyle = style10;
            this.Column13.HeaderText = "Lập";
            this.Column13.Name = "Column13";
            this.Column13.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column13.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column13.Width = 0x35;
            this.Column14.DataPropertyName = "Marked";
            this.Column14.HeaderText = "";
            this.Column14.Name = "Column14";
            this.Column14.Resizable = DataGridViewTriState.False;
            this.Column14.Width = 20;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.ClientSize = new Size(0x31a, 0x214);
            base.Controls.Add(this.grdDetail);
            base.Controls.Add(this.btnClose);
            base.Controls.Add(this.btnMakeCommand);
            base.Controls.Add(this.btnMakeRequire);
            this.MinimumSize = new Size(0x322, 0x236);
            base.Name = "FormCTDHF0Editor";
            this.Text = "C\x00e2n đối hàng hóa";
            ((ISupportInitialize) this.grdDetail).EndInit();
            base.ResumeLayout(false);
        }

        private void oDetailHeader_OnCheckBoxClicked(bool state)
        {
            this.SelectAllDetail(state);
        }

        private void OnFillData()
        {
            this.LocalDetailTable = this.oMethod.GetItemBalance(this.MasterKey);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.Return))
            {
                this.CheckAccepted = true;
                base.Close();
            }
            else if (keyData == Keys.Escape)
            {
                base.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void SelectAllDetail(bool pb_State)
        {
            if ((this.LocalDetailTable != null) && (this.LocalDetailTable.Rows.Count != 0))
            {
                foreach (DataRow row in this.LocalDetailTable.Rows)
                {
                    if (row.RowState != DataRowState.Deleted)
                    {
                        this.SelectOneDetail(row, pb_State);
                    }
                }
            }
        }

        private void SelectOneDetail(DataRow po_Row, bool pb_State)
        {
            if (po_Row != null)
            {
                if (pb_State)
                {
                    po_Row["So_Luong"] = po_Row["So_Luong2"];
                    po_Row["Marked"] = true;
                }
                else
                {
                    po_Row["So_Luong"] = 0;
                    po_Row["Marked"] = false;
                }
            }
        }

        public bool CheckAccepted
        {
            get
            {
                return this.ib_CheckAccepted;
            }
            set
            {
                this.ib_CheckAccepted = value;
            }
        }

        private GridControl DetailGrid
        {
            get
            {
                return this.grdDetail;
            }
        }

        private DataTable LocalDetailTable
        {
            get
            {
                return this.oLocalDetailTable;
            }
            set
            {
                this.oLocalDetailTable = value;
                this.DetailGrid.DataSource = new BindingSource(this.oLocalDetailTable, this.oLocalDetailTable.TableName);
            }
        }

        private DataRow LocalMasterRow
        {
            get
            {
                return this.oLocalMasterRow;
            }
            set
            {
                this.oLocalMasterRow = value;
            }
        }

        private object MasterKey
        {
            get
            {
                return this.is_MasterKey;
            }
            set
            {
                if (this.is_MasterKey != value)
                {
                    this.is_MasterKey = value;
                }
            }
        }
    }
}

